package com.ch_linghu.fanfoudroid.db;

import java.text.ParseException;

import android.database.Cursor;
import android.provider.BaseColumns;
import android.util.Log;

import com.ch_linghu.fanfoudroid.data.Dm;

/**
 * Table - Direct Messages
 * 
 */
public final class MessageTable implements BaseColumns {

	public static final String TAG = "MessageTable";

	public static final int TYPE_GET = 0;
	public static final int TYPE_SENT = 1;

	public static final String TABLE_NAME = "message";
	public static final int MAX_ROW_NUM = 20;

	public static final String FIELD_USER_ID = "uid";
	public static final String FIELD_USER_NAME = "uname";
	public static final String FIELD_FACE = "face";
	public static final String FIELD_C_TIME = "ctime";
	public static final String FIELD_CONTENT = "content";
	public static final String FIELD_SOURCE = "source";
	public static final String FIELD_COMMENT = "comment";
	public static final String FIELD_TRANSPOND_ID = "transpond_id";
	public static final String FIELD_TRANSPOND = "transpond";
	public static final String FIELD_TYPE = "type";
	public static final String FIELD_IS_UNREAD = "is_unread";
	public static final String FIELD_IS_SENT = "is_send";

	public static final String[] TABLE_COLUMNS = new String[] { _ID,
		FIELD_USER_NAME, FIELD_FACE, FIELD_C_TIME,
		FIELD_CONTENT, FIELD_SOURCE, FIELD_COMMENT,FIELD_TRANSPOND_ID,FIELD_TRANSPOND, FIELD_TYPE,FIELD_IS_UNREAD, FIELD_IS_SENT,FIELD_USER_ID };

	public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME
			+ " (" + _ID + " text primary key on conflict replace, "
			+ FIELD_USER_NAME + " text not null, " + FIELD_CONTENT
			+ " text not null, " + FIELD_FACE + " text not null, "
			+ FIELD_SOURCE + " integer not null, " + FIELD_COMMENT
			+ " integer not null, " + FIELD_C_TIME + " date not null, "
			+ FIELD_TRANSPOND_ID + " integer not null, "
			+ FIELD_TRANSPOND + " integer not null, "
			+ FIELD_TYPE + " integer not null, "
			+ FIELD_IS_UNREAD + " boolean not null, " + FIELD_IS_SENT
			+ " boolean not null, "
			+ FIELD_USER_ID + " text)";

	

	/**
	 * TODO: 将游标解析为一条私信
	 * 
	 * @param cursor
	 *            该方法不会关闭游标
	 * @return 成功返回Dm类型的单条数据, 失败返回null
	 */
	public static Dm parseCursor(Cursor cursor) {

		if (null == cursor || 0 == cursor.getCount()) {
			Log.w(TAG, "Cann't parse Cursor, bacause cursor is null or empty.");
			return null;
		}

		Dm dm = new Dm();

		dm.id = cursor.getString(cursor.getColumnIndex(MessageTable._ID));
		dm.uname = cursor.getString(cursor
				.getColumnIndex(MessageTable.FIELD_USER_NAME));
		dm.content = cursor.getString(cursor
				.getColumnIndex(MessageTable.FIELD_CONTENT));
		dm.face = cursor.getString(cursor
				.getColumnIndex(MessageTable.FIELD_FACE));
		dm.isSent = (0 == cursor.getInt(cursor
				.getColumnIndex(MessageTable.FIELD_IS_SENT))) ? false : true;
		try {
			dm.ctime = TwitterDatabase.DB_DATE_FORMATTER.parse(cursor
					.getString(cursor
							.getColumnIndex(MessageTable.FIELD_C_TIME)));
		} catch (ParseException e) {
			Log.w(TAG, "Invalid created at data.");
		}
		dm.userId = cursor.getString(cursor
				.getColumnIndex(MessageTable.FIELD_USER_ID));

		return dm;
	}
}